﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using AppService.Comparer;
using AppService.Context;
using AppService.Model;

namespace AppService.Dao
{
    public class DoubleColorBallDao
    {

        public DoubleColorBall Get(int id)
        {
            var doubleColorBall=new DoubleColorBall();
            try
            {
                using (var efDbContext = new SqliteEfDbContext())
                {
                    doubleColorBall = efDbContext.DoubleColorBalls.FirstOrDefault(x => x.ID == id);
                }
            }
            catch (Exception e)
            {
                return null;
            }

            return doubleColorBall;
        }

        public List<DoubleColorBall> GetList()
        {
            List<DoubleColorBall> doubleColorBallList;
            try
            {
                using (var efDbContext = new SqliteEfDbContext())
                {
                    doubleColorBallList = efDbContext.DoubleColorBalls.OrderByDescending(x=>x.ID).ToList();
                }
            }
            catch (Exception e)
            {
                var mm = e.Message;
                return null;
            }

            return doubleColorBallList;
        }

        public int Insert(DoubleColorBall model)
        {
            try
            {
                using (var efDbContext = new SqliteEfDbContext())
                {
                    efDbContext.DoubleColorBalls.Add(model);
                    efDbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return 0;
            }
            return 1;
        }

        public int Update(DoubleColorBall model)
        {
            try
            {
                using (var efDbContext = new SqliteEfDbContext())
                {
                    efDbContext.DoubleColorBalls.Attach(model);
                    efDbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return 0;
            }

            return 1;
        }
        public int Save(List<DoubleColorBall> list)
        {
            try
            {
                using (var efDbContext = new SqliteEfDbContext())
                {
                    efDbContext.DoubleColorBalls.AddRange(list);
                    efDbContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return 0;
            }

            return 1;
        }


 


    }
}

